Skip to content

Conversation

bnowak
Copy link
Contributor

@bnowak bnowak commented Jan 17, 2025

This PR goal is to determine correct result typing for QueryBus kind of classes which uses SF messenger HandleTrait internally and simply return its results.

Comment on lines +116 to +64
use HandleTrait;

public function dispatch(object $query): mixed
{
return $this->handle($query);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add this test case with changed function name.

Suggested change
use HandleTrait;
public function dispatch(object $query): mixed
{
return $this->handle($query);
}
use HandleTrait {
handle as private handleQuery;
}
public function handle(object $query): mixed
{
return $this->handleQuery($query);
}

@michaljusiega
Copy link

@ondrejmirtes Sorry to disturb you, but I think this is one of the pieces that is missing in this whole puzzle to support. Can we move forward ?

@bnowak
Copy link
Contributor Author

bnowak commented Mar 14, 2025

Hi @michaljusiega, thanks for your reply. I think we can add it - no problem. However, from my perspective it's more important to address the root issue (missing feature) of the topic (described here). My reasoning is without having this result interpreted on bus class level - this is much less useful in general.

@ondrejmirtes
Copy link
Member

So this QueryBus class is something in your codebase and you want the same type inference that classes using HandleTrait and calling the handle method have?

You can simply write a dynamic return type extension for QueryBus::dispatch() that delegates the type resolution to new MethodCall($methodCall->var, 'handle', $methodCall->getArgs()).

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from cc1869e to 9b62bd8 Compare July 25, 2025 09:20
@bnowak bnowak marked this pull request as ready for review July 28, 2025 07:18
@bnowak
Copy link
Contributor Author

bnowak commented Jul 28, 2025

@ondrejmirtes It's ready for review now. You were right about my intentions, however I wanted to make it working for any class that uses HandleTrait internally. I didn't want to hardcoded it, so made it parameterized. I've added also support for interfaces. Please review in you free time 🙏

It's likely to be conflicted with my another PR about splitting tests, so once first of these PR will be merged I can resolve conflicts on another one. 😉

@bnowak
Copy link
Contributor Author

bnowak commented Aug 29, 2025

@ondrejmirtes any chances to review & merge it soon, please? :)

{
public function __invoke(GetProductQuery $query): Product
{
return $this->productRepository->get($query->productId);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add comments to the code in README about the inferred types with and without the extension? I'm still not sure what it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reply.

So it's about inferring types of query bus classes (from CQRS pattern) or any custom class which internally use messenger's HandleTrait to get and return result of any dispatched message. Without the extension phpstan is not able to recognize that and gives generic return type of bus class (which in fact is some more specified query result).

So the main difference is calling query bus classes, where based on passed/dispatched message we're getting its correct result type.

I've updated README a bit and the the direct outcome is shown in the end of it - it shows that we're getting Product type instead of generic mixed. If that's still not precised and unclear, please advice me what else I could add/adjust. Maybe my describing skills are not so good 😅

@bnowak bnowak force-pushed the messenger-handle-trait-generic-typing-support branch from cdcaab5 to a9f07aa Compare September 8, 2025 05:29
@bnowak bnowak requested a review from ondrejmirtes September 10, 2025 07:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants